package com.bigben.controller;

import com.bigben.Exception.DictNotFountException;
import com.bigben.dto.Error;
import com.bigben.entites.CommDictionaryDetail;
import com.bigben.service.DictDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Author: zhaoben
* @Date: 2016/12/18 16:28
* @Desc: 相关操作
*/
@RestController
@RequestMapping("dictDetail")
public class DictDetailController {

    @Autowired
    private DictDetailService dictDetailService;

    /**
     * @Desc: 根据父编码获取对应子节点的字典值
     * @Author: zhaoben
     * @Date: 2016/12/18 16:10
     * @Param: String 类型 prentCode
     * @Result: CommDictionary 列表
     */
    @RequestMapping(value = "/parent/{parentCode}", method = RequestMethod.GET, produces = "application/json")
    public List<CommDictionaryDetail> getDictByParentId(@PathVariable("parentCode") String parentCode) {
        List<CommDictionaryDetail> result = dictDetailService.getDictByParentCode(parentCode);
        if (result.size() == 0) {
            throw new DictNotFountException(parentCode);
        }
        return result;
    }

    /**
     * @Desc: 获取所有数据字典值
     * @Author: zhaoben
     * @Date: 2016/12/18 16:10
     * @Param: null
     * @Result: CommDictionary 列表
     */
    @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
    public List<CommDictionaryDetail> getAll() {
        return dictDetailService.getAll();
    }


    /**
     * DictNotFount异常处理
     *
     * @Author: zhaoben
     * @Date: 2016/12/18 15:36
     */
    @ExceptionHandler(DictNotFountException.class)
    public Error DictNotFound(DictNotFountException e) {
        String userId = e.getDictId();
        Error error = new Error(HttpStatus.NOT_EXTENDED.value(), "ID（" + userId + ") not found");
        return error;
    }
}

